package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.RegisterVo;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //-----------------会员登录-----------------
    @Override
    public String login(UcenterMember member) {
        //获取手机号和密码：传入的数据GET
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空的判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"登录失败");
        }

        //手机号是否正确的判断
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();//mybatis plus中实现查询的对象封装操作类
        wrapper.eq("mobile",mobile);//查手机号
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);//查询出对象，不重复
        //判断对象是否为空
        if (ucenterMember == null) {
            throw new GuliException(20001,"手机号不存在，登录失败");
        }
        //密码是否正确的判断
        //把输入的密码进行加密，再和数据库中的比较
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001,"密码错误，登录失败");
        }
        //账户是否正禁用
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001,"账户已禁用，登录失败");
        }

        //登录成功，返回Token值
        //生成Token字符串，使用JWT工具类
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname());
        return jwtToken;
    }


    /**
     * //-----------------会员注册-----------------
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //1.获取注册信息，进行校验
        String nickname = registerVo.getNickname();//获取昵称
        String mobile = registerVo.getMobile();//获取手机号
        String password = registerVo.getPassword();//获取密码
        String code = registerVo.getCode();//获取验证码

        //2.校验参数,检验（）里面的参数是否为空
        if (StringUtils.isEmpty(nickname) ||
        StringUtils.isEmpty(mobile) ||
        StringUtils.isEmpty(password) ||
        StringUtils.isEmpty(code)) {
            throw new GuliException(20001,"数据校验错误，有空值，请检查");
        }

        //3.校验校验验证码
        //从redis获取发送的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);//根据手机号去存的

        if (!code.equals(redisCode)) {
            throw new GuliException(20001,"验证码校验失败，请检查");
        }
        //4.查询数据库中是否存在相同的手机号码
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);//计算一共有多少符合条件的
        if (count.intValue() > 0) {
            throw new GuliException(20001,"数据库已有手机号");
        }

        //5.添加注册信息到数据库
        //用this.save()也可以
        //创建一个实体类对象存放值
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");//头像
        baseMapper.insert(member);


    }

    @Override
    public UcenterMember getOpenIdMenber(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }


}
